﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;
using System.IO;

public partial class controls_admin_PictureEdit : Admin_Controls_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadGallery();
            loadAdv();
        }
    }

    protected void loadGallery()
    {
        DataTable objDT = BO_portal_galleries.SelectAll(1);
        lbxCategories.DataTextField = "GALLERY_NAME";
        lbxCategories.DataValueField = "GALLERY_ID";
        lbxCategories.DataSource = objDT;
        lbxCategories.DataBind();
    }
    private void loadAdv()
    {
        try
        {
            ObjDT = BO_portal_galleries_details.Search(Id);
            if (ObjDT != null && ObjDT.Rows.Count > 0)
            {
                if (!ObjDT.Rows[0]["PICTURE_PATH"].ToString().Equals(string.Empty))
                {
                    if (ObjDT.Rows[0]["PICTURE_PATH"].ToString().StartsWith("http://"))
                    {
                        txtDirectLink.Text = ObjDT.Rows[0]["PICTURE_PATH"].ToString();
                    }
                    string extension = Path.GetExtension(ObjDT.Rows[0]["PICTURE_PATH"].ToString());
                    if (extension == ".swf")
                        imgAdv.InnerHtml = "<object height=' 100' border='0' width='200' codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"><param value=\"" + ObjDT.Rows[0]["PICTURE_PATH"].ToString() + "\" name=\"movie\"><param value=\"always\" name=\"AllowScriptAccess\"><param value=\"High\" name=\"quality\"><param value=\"transparent\" name=\"wmode\"><param value=\"#\" name=\"FlashVars\"><embed  height=' 100' border='0' width='300' allowscriptaccess=\"always\" wmode=\"transparent\" loop=\"true\" play=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" flashvars=\"#\" src=\"" + ObjDT.Rows[0]["PICTURE_PATH"].ToString() + "\"></object>";
                    else
                    {
                        if (!ObjDT.Rows[0]["PICTURE_PATH"].ToString().StartsWith("http://"))
                            imgAdv.InnerHtml = "<img src='/" + Setting.Path + ObjDT.Rows[0]["PICTURE_PATH"].ToString() + "' height='100' onerror='ImgError(this);' />";
                        else
                            imgAdv.InnerHtml = "<img src='" + ObjDT.Rows[0]["PICTURE_PATH"].ToString() + "' height='100' onerror='ImgError(this);' />";

                    }
                }
                txtTitle.Text = ObjDT.Rows[0]["PICTURE_NAME"].ToString();
                lbxCategories.SelectedValue = ObjDT.Rows[0]["GALLERY_ID"].ToString();
                chbDisplay.Checked = Convert.ToBoolean(ObjDT.Rows[0]["IS_DISPLAY_ON_HOME_PAGE"]);
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message); }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ObjDT = BO_portal_galleries_details.Search(Id);
        string fileName = String.Empty;
        fileName = base.Upload("gallery", ObjDT.Rows[0]["PICTURE_PATH"].ToString(), fulAdv, divMessage, false);
        if (!fulAdv.HasFile)
        {
            fileName = txtDirectLink.Text;
        }
        try
        {
            string title = txtTitle.Text.Trim();
            bool display = chbDisplay.Checked;
            DateTime endDate = DateTime.Now;
            DateTime startDate = DateTime.Now;
            string result = BO_portal_galleries_details.Update(Id, Convert.ToInt32(lbxCategories.SelectedValue), title
                    , fileName, 0, chbDisplay.Checked, 0, Convert.ToDateTime(ObjDT.Rows[0]["CREATED_DATE"]), ObjDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, Administrator.USER_NAME, true);
            if (result.Equals("0"))
                divMessage.InnerHtml = MessageBox.Error("Thông Báo", "Lỗi trong quá trình truyền tải dữ liệu. Mong bạn trở lại sau .");
            else
            {
                divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Thực hiện thành công .");
                Response.Redirect("Default.aspx?control=Galleries&keyword=" + Keyword);
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi xảy ra !<br />" + ex.Message);
        }
    }
}
